﻿using IceDog.EFCore.Models.Blog;
using IceDog.EFCore.WebApiServer.Context;
using System;
using System.Linq;

namespace IceDog.EFCore.WebApiServer
{
    public class DbInitializer
    {
        public static void Initialize(DioBlogContext context)
        {
            // Look for any students.
            if (context.BlogArticles.Any())
            {
                return;   // DB has been seeded
            }

            var userIsExist = context.BlogUsers.Any(u => u.Account == "晓晨master");
            if (userIsExist) { return; }
            var user = new BlogUser()
            {
                Account = "晓晨master",
                BlogUrl = "www.example.com/xiaochenmaster",
            };
            context.BlogUsers.Add(user);
            var count = context.SaveChanges();
            Console.WriteLine("{0} records saved to database", count);
            var article = new BlogArticle
            {
                Author = user,
                CreateTime = DateTime.Now,
                Title = "EF Core 入门系列 1",
                Content = "入门。。。。"
            };
            context.BlogArticles.Add(article);
            count = context.SaveChanges();
            Console.WriteLine("{0} records saved to database", count);
            var comment = new BlogComment()
            {
                CommentTime = DateTime.Now,
                Content = "大佬厉害",
                Article = article,
                Commenter = user
            };
            context.BlogComments.Add(comment);
            count = context.SaveChanges();
            Console.WriteLine("{0} records saved to database", count);
        }
    }
}
